﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TIENITCodeGener
{
    public partial class frmCommon : Form
    {

#region "Constructor"
        public frmCommon()
        {
            InitializeComponent();
            ITFACommon.SetFormIcon(this);
            SetLanguage();
        }
#endregion

#region "Form Events"
        private void frmCommon_Load(object sender, EventArgs e)
        {
            try
            {
                Common_LoadSetting();
            }
            catch (System.Exception ex)
            {
                ITFACommon.Message_Error(ex);
            }
        }

        private void frmCommon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
#endregion

#region "Common Functions"
        private void Common_LoadSetting()
        {
            Common_Set_GenCodeOption_ToControl(rb_Common_Lang_NET, rb_Common_Lang_Java, rb_Common_DBMS_SS, rb_Common_DBMS_ORA, chk_Common_Platform_WEB, chk_Common_Platform_WIN);
            chk_Common_DNN_Supported.Checked = TIENITCodeGener.Properties.Settings.Default.DotNetNuke_Supported;
            txt_Common_Author.Text = TIENITCodeGener.Properties.Settings.Default.Author;

            if (rb_Common_DBMS_SS.Checked == true)
            {
                txt_Common_Namespace.Text = TIENITCodeGener.Properties.Settings.Default.DefaultNamespaceForNet;
            }
            else if (rb_Common_DBMS_ORA.Checked == true)
            {
                txt_Common_Namespace.Text = TIENITCodeGener.Properties.Settings.Default.DefaultNamespaceForOra;
            }

            if (TIENITCodeGener.Properties.Settings.Default.NET_Language.Equals("VB"))
            {
                cmb_NET_Language.SelectedIndex = 0;
            }
            else if (TIENITCodeGener.Properties.Settings.Default.NET_Language.Equals("CS"))
            {
                cmb_NET_Language.SelectedIndex = 1;
            }

            //check free or full
            if (TIENITCodeGener.Properties.Settings.Default.Mode_App.Equals("FREE"))
            {
                chk_Common_DNN_Supported.Enabled = false;
                cmb_NET_Language.Enabled = false;
            }

            //in completing ...
            //DESKTOP: DOTNET, JAVA
            //DEVICES: WINDOWS_PHONE, IOS, ANDROID
            if (TIENITCodeGener.Properties.Settings.Default.Main_Catalog.Equals("DESKTOP"))
            {
                //DOTNET, JAVA
                if (TIENITCodeGener.Properties.Settings.Default.Sub_Catalog.Equals("DOTNET"))
                {
                    rb_Common_Lang_NET.Visible = true;
                    cmb_NET_Language.Visible = true;
                    rb_Common_Lang_Java.Visible = false;
                }
                else if (TIENITCodeGener.Properties.Settings.Default.Sub_Catalog.Equals("JAVA"))
                {
                    rb_Common_Lang_NET.Visible = false;
                    cmb_NET_Language.Visible = false;
                    rb_Common_Lang_Java.Visible = true;
                }
            }
        }

        private string Common_Get_NetLanguage_FromControl(ComboBox cmb)
        {
            string strLang = "VB";
            if (cmb_NET_Language.SelectedIndex == 0) //VB
            {
                strLang = "VB";
            }
            else if (cmb_NET_Language.SelectedIndex == 1) //CS
            {
                strLang = "CS";
            }

            return strLang;
        }

        private int Common_Get_WebOrWinOrAll_FromControl(CheckBox cWeb, CheckBox cWin)
        {
            //1:Web, 2:Win, 3:Web and Win
            int web_or_win_or_all = 1;

            if (cWeb.Checked && !cWin.Checked)
            {
                web_or_win_or_all = 1;
            }
            else if (!cWeb.Checked && cWin.Checked)
            {
                web_or_win_or_all = 2;
            }
            else if (cWeb.Checked && cWin.Checked)
            {
                web_or_win_or_all = 3;
            }
            return web_or_win_or_all;
        }

        private string Common_Get_GenCodeOption_FromControl(RadioButton rbNET, RadioButton rbJAVA, RadioButton rbSS, RadioButton rbORA, CheckBox cWeb, CheckBox cWin)
        {
            string str = "";
            if (rbNET.Checked) //NET
            {
                if (rbSS.Checked) //SQL SERVER
                {
                    if (cWeb.Checked && !cWin.Checked)
                    {
                        str = "111";
                    }
                    else if (!cWeb.Checked && cWin.Checked)
                    {
                        str = "112";
                    }
                    else if (cWeb.Checked && cWin.Checked)
                    {
                        str = "111112";
                    }
                }
                else if (rbORA.Checked) //ORACLE
                {
                    if (cWeb.Checked && !cWin.Checked)
                    {
                        str = "121";
                    }
                    else if (!cWeb.Checked && cWin.Checked)
                    {
                        str = "122";
                    }
                    else if (cWeb.Checked && cWin.Checked)
                    {
                        str = "121122";
                    }
                }
            }
            else if (rbJAVA.Checked) //JAVA
            {
                if (rbSS.Checked) //SQL SERVER
                {
                    if (cWeb.Checked && !cWin.Checked)
                    {
                        str = "211";
                    }
                    else if (!cWeb.Checked && cWin.Checked)
                    {
                        str = "212";
                    }
                    else if (cWeb.Checked && cWin.Checked)
                    {
                        str = "211212";
                    }
                }
                else if (rbORA.Checked) //ORACLE
                {
                    if (cWeb.Checked && !cWin.Checked)
                    {
                        str = "221";
                    }
                    else if (!cWeb.Checked && cWin.Checked)
                    {
                        str = "222";
                    }
                    else if (cWeb.Checked && cWin.Checked)
                    {
                        str = "221222";
                    }
                }
            }
            return str;
        }

        private void Common_Set_GenCodeOption_ToControl(RadioButton rbNET, RadioButton rbJAVA, RadioButton rbSS, RadioButton rbORA, CheckBox cWeb, CheckBox cWin)
        {
            //1:Web, 2:Win, 3:Web and Win
            string genOption = "";
            int web_or_win_or_all = TIENITCodeGener.Properties.Settings.Default.Web_Or_Win_Or_All;

            if (TIENITCodeGener.Properties.Settings.Default.Sub_Catalog.Equals("DOTNET"))
            {
                genOption = TIENITCodeGener.Properties.Settings.Default.Option_For_Gen_Code;
            }
            else if (TIENITCodeGener.Properties.Settings.Default.Sub_Catalog.Equals("JAVA"))
            {
                genOption = TIENITCodeGener.Properties.Settings.Default.Option_For_Gen_Code_J;
            }
            
            if (genOption.Equals("111") || genOption.Equals("112") || genOption.Equals("111112"))
            {
                rbNET.Checked = true; //NET
                rbSS.Checked = true; //SS
            }
            else if (genOption.Equals("121") || genOption.Equals("122") || genOption.Equals("121122"))
            {
                rbNET.Checked = true; //NET
                rbORA.Checked = true; //ORA
            }
            else if (genOption.Equals("211") || genOption.Equals("212") || genOption.Equals("211212"))
            {
                rbJAVA.Checked = true; //JAVA
                rbSS.Checked = true; //SS
            }
            else if (genOption.Equals("221") || genOption.Equals("222") || genOption.Equals("221222"))
            {
                rbJAVA.Checked = true; //JAVA
                rbORA.Checked = true; //ORA
            }

            if (web_or_win_or_all == 1) //web
            {
                cWeb.Checked = true;
                cWin.Checked = false;
            }
            else if (web_or_win_or_all == 2) //win
            {
                cWeb.Checked = false;
                cWin.Checked = true;
            }
            else if (web_or_win_or_all == 3) //web and win
            {
                cWeb.Checked = true;
                cWin.Checked = true;
            }

            cmb_NET_Language.Enabled = rbNET.Checked;
        }

        private bool Common_ValidateSaving()
        {
            bool bReturn = true;
            if (!chk_Common_Platform_WEB.Checked && !chk_Common_Platform_WIN.Checked)
            {
                bReturn = false;
            }
            return bReturn;
        }

        private void SetLanguage()
        {
            if (TIENITCodeGener.Properties.Settings.Default.Soft_Language.Equals("EN-US"))
            {
                lbl_Title.Text = "Common Setting";
                this.Text = "Common Setting";
                gb_CodeLegal.Text = "Code Legal Info";
                gb_ProgramLang.Text = "Programing Language";
                gb_RDBMS.Text = "RDBMS";
                gb_Platform.Text = "Platform";
                gb_DotNetNuke.Text = "DotNetNuke";
                btnSave.Text = "Save";
                chk_Common_DNN_Supported.Text = "Supported";
            }
            else if (TIENITCodeGener.Properties.Settings.Default.Soft_Language.Equals("VI-VN"))
            {
                lbl_Title.Text = "Thiết Lập Chung";
                this.Text = "Thiết Lập Chung";
                gb_CodeLegal.Text = "Chú thích mã nguồn";
                gb_ProgramLang.Text = "Ngôn ngữ lập trình";
                gb_RDBMS.Text = "Cơ sở dữ liệu";
                gb_Platform.Text = "Nền";
                gb_DotNetNuke.Text = "DotNetNuke";
                btnSave.Text = "Lưu";
                chk_Common_DNN_Supported.Text = "Hỗ trợ";
            }
        }
#endregion

#region "Save Settings"
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (Common_ValidateSaving())
                {
                    TIENITCodeGener.Properties.Settings.Default.Author = txt_Common_Author.Text;

                    if (rb_Common_DBMS_SS.Checked == true)
                    {
                        TIENITCodeGener.Properties.Settings.Default.DefaultNamespaceForNet = txt_Common_Namespace.Text;
                    }
                    else if (rb_Common_DBMS_ORA.Checked == true)
                    {
                        TIENITCodeGener.Properties.Settings.Default.DefaultNamespaceForOra = txt_Common_Namespace.Text;
                    }
                    
                    if (TIENITCodeGener.Properties.Settings.Default.Sub_Catalog.Equals("DOTNET"))
                    {
                        TIENITCodeGener.Properties.Settings.Default.Option_For_Gen_Code = Common_Get_GenCodeOption_FromControl(rb_Common_Lang_NET, rb_Common_Lang_Java, rb_Common_DBMS_SS, rb_Common_DBMS_ORA, chk_Common_Platform_WEB, chk_Common_Platform_WIN);
                    }
                    else if (TIENITCodeGener.Properties.Settings.Default.Sub_Catalog.Equals("JAVA"))
                    {
                        TIENITCodeGener.Properties.Settings.Default.Option_For_Gen_Code_J = Common_Get_GenCodeOption_FromControl(rb_Common_Lang_NET, rb_Common_Lang_Java, rb_Common_DBMS_SS, rb_Common_DBMS_ORA, chk_Common_Platform_WEB, chk_Common_Platform_WIN);
                    }

                    TIENITCodeGener.Properties.Settings.Default.Web_Or_Win_Or_All = Common_Get_WebOrWinOrAll_FromControl(chk_Common_Platform_WEB, chk_Common_Platform_WIN);
                    TIENITCodeGener.Properties.Settings.Default.NET_Language = Common_Get_NetLanguage_FromControl(cmb_NET_Language);
                    TIENITCodeGener.Properties.Settings.Default.DotNetNuke_Supported = chk_Common_DNN_Supported.Checked;
                    TIENITCodeGener.Properties.Settings.Default.Save();

                    this.DialogResult = DialogResult.OK;
                    //this.Close();
                }
                else
                {
                    ITFACommon.Message_Information("At least ONE platform must be choose !");
                }
            }
            catch (System.Exception ex)
            {
                ITFACommon.Message_Error(ex);
            }
        }

        private void rb_Common_DBMS_SS_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_Common_DBMS_SS.Checked == true)
            {
                txt_Common_Namespace.Text = TIENITCodeGener.Properties.Settings.Default.DefaultNamespaceForNet;
            }
        }

        private void rb_Common_DBMS_ORA_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_Common_DBMS_ORA.Checked == true)
            {
                txt_Common_Namespace.Text = TIENITCodeGener.Properties.Settings.Default.DefaultNamespaceForOra;
            }
        }
#endregion

    }
}
